<?php

/**
 * @filename common.php 
 * @encoding UTF-8 
 * @author nemo.xiaolan <a href="mailto:335454250@qq.com">335454250@qq.com</a>
 * @link <a href="http://www.sep-v.com">http://www.sep-v.com</a>
 * @license http://www.sep-v.com/code-license
 * @datetime 2013-11-17 10:35:33
 * @description
 * 
 */
function makeBillCode() {
    $year_code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
    $order_sn = $year_code[intval(date('Y')) - 2010] .
            strtoupper(dechex(date('m'))) . date('d') .
            substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%d', rand(0, 99));
    return $order_sn;
}

function getUserCache() {
    $userData = F("User/All");
    if (!$userData) {
        $tmp = D("User")->select();
        foreach ($tmp as $v) {
            unset($v["password"]);
            $userData[$v["id"]] = $v;
        }
        F("User/All", $userData);
    }
    return $userData;
}

function getCurrentUid() {
    return $_SESSION["user"]["id"];
}

function toDate($str, $format = "Y-m-d H:i:s") {
    return date($format, $str);
}

function toUsername($uid) {
    if (!$uid) {
        return L("Unnamed");
    }
    $userData = getUserCache();
    return $userData[$uid]["username"];
}

function toTruename($uid) {
    if (!$uid) {
        return "";
    }
    $userData = getUserCache();
    return $userData[$uid]["truename"];
}

function toCustomerName($customerId) {
    $cusData = F("Customer/All");
    if (!$cusData) {
        $model = D("Customer");
        $tmp = $model->select();
        foreach ($tmp as $v) {
            $cusData[$v["id"]] = $v["name"];
        }
        F("Customer/All", $cusData);
    }
    return $cusData[$customerId];
}

function clearCache($type = 0, $path = NULL) {
    if (is_null($path)) {
        switch ($type) {
            case 0:// 模版缓存目录
                $path = CACHE_PATH;
                break;
            case 1:// 数据缓存目录
                $path = TEMP_PATH;
                break;
            case 2:// 日志目录
                $path = LOG_PATH;
                break;
            case 3:// 数据目录
                $path = DATA_PATH;
                break;
        }
    }
//    import("ORG.Io.Dir");
    $rs = delDirAndFile($path);
}

function delDirAndFile($dirName) {
    if ($handle = opendir("$dirName")) {
        while (false !== ( $item = readdir($handle) )) {
            if ($item != "." && $item != "..") {
                if (is_dir("$dirName/$item")) {
                    delDirAndFile("$dirName/$item");
                } else {
                    @ unlink("$dirName/$item");
                }
            }
        }
        @ closedir($handle);
        @ rmdir($dirName);
    }
}

function toSaleType($id) {
    $saleType = F("SaleType");
    if (!$saleType) {
        $model = D("SaleType");
        $tmp = $model->select();
        foreach ($tmp as $v) {
            $saleType[$v["id"]] = $v["name"];
        }
        F("SaleType", $tmp);
    }
//    print_r($saleType);exit;
    return $saleType[$id];
}

function toReturnsType($id) {
    $returnsType = F("ReturnsType");
    if (!$returnsType) {
        $model = D("ReturnsType");
        $tmp = $model->select();
        foreach ($tmp as $v) {
            $returnsType[$v["id"]] = $v["name"];
        }
        F("ReturnsType", $returnsType);
    }
//    print_r($saleType);exit;
    return $returnsType[$id];
}

function makeDBConfigCache() {
    $config = F("Config/DB");
    if (!$config) {
        $model = D("Config");
        $tmp = $model->select();
        foreach ($tmp as $v) {
            $config[$v["name"]] = $v["value"];
        }
    }
    return $config;
}

function DBC($name) {
    $config = makeDBConfigCache();
    return $config[$name];
}

function getPwd($source) {
    return md5($source);
}

function getFactoryCodeAll($params) {
    list($fcid, $colorid, $standardid) = $params;
    return implode("-", $params);
}

function extraFactoryCodeAll($factory_code_all) {
    list($fcid, $colorid, $standardid) = explode("-", $factory_code_all);
    $data = array(
        "factory_code" => $fcid,
        "color_id" => $colorid,
        "standard_id" => $standardid
    );
    return $data;
//    print_r($data);exit;
}

/**
 * 仓库列表
 */
function getStocks() {
    $stocks = F("Stock/List");
    if (!$stocks) {
        $model = D("Stock");
        $stocks = $model->select();
        $stocks = $model->getIndexArray($stocks);
        F("Stock/List", $stocks);
    }
    return $stocks;
}

function toStockName($id) {
    $stocks = getStocks();
    return $stocks[$id];
}

function checkStoreNum($factory_code_all, $num) {
    $model = D("StockProductList");
    $data = $model->where("factory_code_all='" . $factory_code_all . "'")->find();
    return $data["num"] >= $num;
}

/**
 * 生成日期序列
 */
function makeDateRange($start, $end, $step, $format = "m-d") {
    $tmp = range($start, $end, $step);
//    echo "<pre>";
//    echo $start;
//    echo "<br />";
//    echo $end;
//    echo "<br />";
//    echo $step;
//    var_dump($tmp);
    foreach ($tmp as $v) {
        $dateRange[] = date($format, $v);
    }
    return $dateRange;
}

function ksortHaveNoIndex($data) {
    foreach ($data as $v) {
        $tmp[] = $v;
    }
    return $tmp;
}

function array_sort($arr, $keys, $type = 'asc') {
    $keysvalue = $new_array = array();
    foreach ($arr as $k => $v) {
        $keysvalue[$k] = $v[$keys];
    }
    if ($type == 'asc') {
        asort($keysvalue);
    } else {
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k => $v) {
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}

?>
